﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static _01_Tower3._0.Game;

namespace _01_Tower3._0
{
    internal class SceneManger : IGame
    {
        static public SceneManger? instance = null;
        public static SceneManger Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SceneManger();
                }
                return instance;
            }
        }

        IScene[] scenes;
        ToolMenu toolMenu;
        CombatSystem cs;
        Map map;
        static public Hero hero;

        DialogSystem ds;

        int currentSceneIndex;

        public static string sceneName;

        private SceneManger()
        {
            
        }
        public void Init()
        {
            string[] paths = { "../../../map.csv", "../../../map2.csv", "../../../map3.csv" };
            map = new Map(paths, 60, 30);
            // todo 学过数据结构之后优化 注册
            cs = new CombatSystem();
            ds = new DialogSystem();

            hero = new Hero("你", 100, 100, 20, 20, map);
            toolMenu = new ToolMenu(84, 12, 12, 15, 10, hero);
            scenes = new IScene[5];
            scenes[0] = new SceneMenu();
            scenes[1] = new SceneMap1(cs, toolMenu, map, ds, hero);
            scenes[2] = new SceneCombat(cs);
            scenes[3] = new SceneBag(toolMenu, map, cs);
            scenes[4] = new SceneKingDialog(ds);
            currentSceneIndex = 0;


            map.ChangeMap(0);
            for (int i = 0; i < scenes.Length; i++)
            {
                scenes[i].Init();
            }
        }

        public void Input()
        {
            scenes[currentSceneIndex].Input();
        }

        public void Render()
        {
            scenes[currentSceneIndex].Render();
            //for (int i = 0; i < scenes.Length; i++)
            //{
            //    scenes[i].Render();
            //}
        }

        public void Update()
        {
            scenes[currentSceneIndex].Update();
            //for (int i = 0; i < scenes.Length; i++)
            //{
            //    scenes[i].Update();
            //}
        }

        public void FixUpdate()
        {
            for (int i = 0; i < scenes.Length; i++)
            {
                scenes[i].FixUpdate();
            }
        }

        public void End()
        {
            for (int i = 0; i < scenes.Length; i++)
            {
                scenes[i].End();
            }
        }

        public void ChangeSceneByName(string name)
        {
            for (int i = 0; i < scenes.Length; i++)
            {
                if (scenes[i].Name == name)
                {
                    currentSceneIndex = i;
                    Console.Clear();
                    return;
                }
            }
        }
    }
}
